<template>
  <div class="msg-bg">
    <div class="msg-header">消息中心</div>
    <!-- 聊天入口 -->
    <div class="chat-entry" @click="goToChatList">
      <div class="chat-entry-icon">
        <van-icon name="chat-o" />
      </div>
      <div class="chat-entry-content">
        <div class="chat-entry-title">聊天</div>
        <div class="chat-entry-desc">与同事沟通交流</div>
      </div>
      <van-icon name="arrow" class="chat-entry-arrow" />
    </div>

    <div class="msg-list">
      <van-swipe-cell
        v-for="(msg, index) in messages"
        :key="msg.id"
        :right-width="80"
      >
        <template #right>
          <van-button
            square
            type="danger"
            text="删除"
            @click.stop="onDelete(msg, index)"
          />
        </template>
        <div class="msg-item" @click="goDetail(msg)">
          <img class="msg-avatar" :src="msg.avatar" />
          <div class="msg-content">
            <div class="msg-title-row">
              <span class="msg-title">{{ msg.title }}</span>
              <span v-if="msg.unread" class="msg-dot"></span>
            </div>
            <div class="msg-bottom-row">
              <span class="msg-desc">{{ msg.desc }}</span>
              <span class="msg-time">{{ msg.time }}</span>
            </div>
          </div>
        </div>
      </van-swipe-cell>
    </div>
  </div>
</template>

<script>
import { getH5MessageList } from "@/api/api.js";
import systemAvatar from "@/assets/system.png";
import defaultAvatar from "@/assets/avatar.png";

export default {
  name: "message",
  data() {
    return {
      messages: [],
    };
  },
  created() {
    this.fetchMessages();
  },
  methods: {
    async fetchMessages() {
      try {
        const res = await getH5MessageList();
        console.log("res:", JSON.stringify(res));
        if (res.data && res.status === 0) {
          // 适配接口字段
          this.messages = (res.data || []).map((item) => ({
            id: item.id,
            title: item.title,
            desc: item.content,
            avatar: item.senderId === "SYSTEM" ? systemAvatar : defaultAvatar,
            unread: item.status === "UNREAD",
            time: item.createTime,
            messageType: item.messageType,
            url: item.url,
            processInstanceId: item.processInstanceId,
            taskId: item.taskId,
          }));
        } else {
          this.$toast("获取消息失败");
        }
      } catch (e) {
        console.log(e);
        this.$toast("获取消息异常");
      }
    },
    goDetail(msg) {
      if (msg.messageType === "TASK") {
        // 跳转流程详情
        this.$router.push({
          path: "/flowDetail",
          query: {
            processInstanceId: msg.processInstanceId || msg.id,
            taskId: msg.taskId,
          },
        });
      } else if (msg.messageType === "NOTICE") {
        // 跳转到消息详情页
        this.$router.push({
          path: "/messageDetail",
          query: { id: msg.id },
        });
      } else {
        this.$toast("暂不支持该类型消息跳转");
      }
    },
    goToChatList() {
      this.$router.push("/chatList");
    },
    onDelete(msg, index) {
      this.$dialog
        .confirm({
          title: "确认删除",
          message: "确定要删除这条消息吗？",
        })
        .then(() => {
          this.messages.splice(index, 1);
          this.$toast("已删除");
        });
    },
  },
};
</script>

<style scoped>
.msg-bg {
  min-height: 100vh;
  background: #fff;
  padding-bottom: 40px;
  overflow-x: hidden;
}
.msg-header {
  background: #1677ff;
  color: #fff;
  font-size: 22px;
  font-weight: bold;
  text-align: center;
  padding: 18px 0 12px 0;
  letter-spacing: 2px;
  position: relative;
}
.msg-list {
  margin: 18px 12px 0 12px;
  overflow-x: hidden;
}
.van-swipe-cell {
  background: transparent;
  margin-bottom: 16px;
  border-radius: 14px;
  box-shadow: 0 2px 8px #1677ff11;
}
.van-swipe-cell__wrapper {
  border-radius: 14px;
}
.msg-item {
  display: flex;
  align-items: flex-start;
  background: #fff;
  border-radius: 14px;
  box-shadow: none;
  padding: 14px 16px;
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 0;
  min-height: 64px;
  transition: background 0.2s;
}
.van-swipe-cell__right {
  display: flex;
  align-items: stretch;
  height: 100%;
}
.van-button--danger {
  border-radius: 0 14px 14px 0 !important;
  height: 100%;
  min-width: 80px;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.msg-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  margin-right: 14px;
  object-fit: cover;
  background: #f2f2f2;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px #1677ff22;
}
.msg-content {
  flex: 1;
  min-width: 0;
}
.msg-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
}
.msg-title {
  font-size: 16px;
  font-weight: 600;
  color: #222;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}
.msg-dot {
  width: 9px;
  height: 9px;
  background: #ff3b30;
  border-radius: 50%;
  display: inline-block;
  margin-left: 4px;
  box-shadow: 0 0 4px #ff3b3033;
}
.msg-bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.msg-desc {
  font-size: 14px;
  color: #888;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-time {
  color: #bbb;
  font-size: 13px;
  margin-left: 12px;
  white-space: nowrap;
  flex-shrink: 0;
}

.chat-entry {
  display: flex;
  align-items: center;
  background: linear-gradient(135deg, #1677ff 0%, #42d0ea 100%);
  color: #fff;
  border-radius: 16px;
  padding: 16px 20px;
  margin: 18px 12px 24px 12px;
  cursor: pointer;
  box-shadow: 0 4px 16px #1677ff33;
  transition: transform 0.2s, box-shadow 0.2s;
}

.chat-entry:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px #1677ff44;
}

.chat-entry-icon {
  font-size: 24px;
  margin-right: 12px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-entry-content {
  flex: 1;
}

.chat-entry-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 2px;
}

.chat-entry-desc {
  font-size: 13px;
  opacity: 0.9;
}

.chat-entry-arrow {
  font-size: 16px;
  opacity: 0.8;
}
</style> 